package evaluation.results;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class WebsiteMaker {

	private static Set<String> toProcess;

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		toProcess = loadToProcess("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/training");
		
		String type = "training";
		
		File f = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/QueryResults/"+type+"/");

		File output = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/Evaluation/results/" + type +"/");
		
		File[] folders = f.listFiles(new FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				return toProcess.contains(pathname.getName());
			}
		});
		
		Comparator<File> c = new Comparator<File>() {

			@Override
			public int compare(File arg0, File arg1) {
				
				return Double.compare(Double.valueOf(arg0.getName().replace(".html", "")), Double.valueOf(arg1.getName().replace(".html", "")));
				
			}
		};
		
		Arrays.sort(folders,c);
				
		for (int i = 0; i < folders.length; i++) {
			
			String name = folders[i].getName();
			
			File[] files = folders[i].listFiles();
			
			Arrays.sort(files,c);
			
			File outp = new File(output,name);
			
			outp.mkdir();
			
			for (int j = 0; j < files.length; j++) {
				
				File outputFile = new File(outp,files[j].getName());
				
				generateWebsite(outputFile,files[j],getPreviousIndex(j,files),getNextIndex(j,files),files,getPreviousIndex(i,folders),getNextIndex(i,folders),folders);
				
			}
			
		}
		
	}

	private static void generateWebsite(File outputFile, File file,
			int previousIndex, int nextIndex, File[] files, int previousIndex2,
			int nextIndex2, File[] folders) throws IOException {
		
		BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
		
		bw.write("<html>\n");
		bw.write("<body>\n");
		bw.write("<table border=\"1\" width=\"100%\" height=\"100%\">\n");
		bw.write("<tr height=\"5%\">\n");
		bw.write("<td width=\"100%\">\n");
		bw.write("<h2>\n");
		bw.write(file.getName());
		bw.write("</h2>\n");
		bw.write("</td>\n");
		bw.write("</tr>\n");
		bw.write("<tr height=\"85%\">\n");
		bw.write("<td width=\"100%\">\n");bw.write("<frame>\n");bw.write("<object data=\""+ file.getAbsolutePath() +"\" width=\"100%\" height=\"100%\">\n"); bw.write("<embed src=\""+file.getAbsolutePath()+"\" width=\"100%\" height=\"100%\">\n"); bw.write("</embed>\n"); 
		bw.write("Error: Embedded data could not be displayed.");
		bw.write("</object>\n");bw.write("</frame>\n");bw.write("</td>\n");
		bw.write("</tr>\n");
		bw.write("<tr height=\"5%\">\n");
		bw.write("<td colspan=2>\n");
		bw.write("<table border=\"2\" width=\"100%\" height=\"100%\">\n");
		bw.write("</tr>\n");
		bw.write("<td width=\"50%\" align=\"right\">\n");
		bw.write("<form>\n");
		bw.write("<input type=\"button\" onClick=\"parent.location='"+files[previousIndex].getName()+"'\" value=\"Previous\">\n");
		bw.write("</form>\n");
		bw.write("</td>\n");
		bw.write("<td width=\"50%\">\n");
		bw.write("<form>\n");
		bw.write("<input type=\"button\" onClick=\"parent.location='"+files[nextIndex].getName()+"'\" value=\"Next\">\n");
		bw.write("</form>\n");
		bw.write("</td>\n");
		bw.write("</tr>\n");
		bw.write("</table>\n");
		bw.write("</td>\n");
		bw.write("</tr>\n");
		bw.write("<tr height=\"5%\">\n");
		bw.write("<td colspan=2>\n");
		bw.write("<table border=\"2\" width=\"100%\" height=\"100%\">\n");
		bw.write("</tr>\n");
		bw.write("<td width=\"50%\" align=\"right\">\n");
		bw.write("<form>\n");
		bw.write("<input type=\"button\" onClick=\"parent.location='../"+folders[previousIndex2].getName()+"/0.html'\" value=\"Previous\">\n");
		bw.write("</form>\n");
		bw.write("</td>\n");
		bw.write("<td width=\"50%\">\n");
		bw.write("<form>\n");
		bw.write("<input type=\"button\" onClick=\"parent.location='../"+folders[nextIndex2].getName()+"/0.html'\" value=\"Next\">\n");
		bw.write("</form>\n");
		bw.write("</td>\n");
		bw.write("</tr>\n");
		bw.write("</table>\n");
		bw.write("</td>\n");
		bw.write("</tr>\n");
		bw.write("</table>\n");
		bw.write("</body>\n");
		bw.write("</html>\n");
		
		
		bw.close();
		
	}

	private static int getNextIndex(int i, File[] array) {
		
		if (i+1 == array.length){
			return 0; 
		}
		return i+1;
		
	}

	private static int getPreviousIndex(int i, File[] array) {
		
		if (i-1 < 0){
			return array.length-1;
		}
		return i-1;
	}

	private static Set<String> loadToProcess(String fileName) throws IOException {
		
		Set<String> set = new HashSet<String>();
		
		BufferedReader br = new BufferedReader(new FileReader(fileName));
		
		String line;
		
		while((line=br.readLine())!=null){
			
			set.add(line);
			
		}
		
		br.close();
		
		return set;
		
	}
	
}
